<?php

/**
 * 文章分类
 * @author pirate
 */
class Admin_ArticlecatController extends Edm_Controller_Action {
    /* 分类列表 */

    public function indexAction() {
        $this->view->menu = array('添加分类' => '/admin/articlecat/add');
        $this->view->ur_here = '分类管理';

        /* 搜索 */
        $filter = new Zend_Filter_StripTags();
        $search = array();
        if ($this->_request->get('keywords')) {
            $search['keywords'] = $filter->filter($this->_request->get('keywords'));
        }

        $this->view->uri = $this->common->getUrl();
        $this->view->catlist = $this->_articlecat->getArticleCat(0, 0, array('*'));
    }

    /* 分类添加、编辑、更新 */

    public function addAction() {
        /* 如果是编辑 获取信息 */
        $act = $this->_request->get('act');
        if ($act == 'edit') {
            $cat_id = $this->_request->get('cat_id');
            $uri = $this->_request->get('fromurl');
            $this->view->menu = array('分类列表' => urldecode($uri));
            $this->view->ur_here = '分类编辑';
            $this->view->catlist = $this->_articlecat->getArticleCat();
            $this->view->thisUrl = $this->common->getUrl();
            if (!empty($cat_id) && !empty($uri)) {
                $catInfo = $this->_articlecat->getCatInfo($cat_id);
                $catInfo['act'] = 'save';
                $this->view->catInfo = $catInfo;
                $this->view->uri = $uri;
            }
        }

        /* 添加时 */
        if (!$act) {
            /* 获取分类 */
            $this->view->catlist = $this->_articlecat->getArticleCat();
            $this->view->uri = $this->common->getUrl();
            $this->view->menu = array('分类列表' => '/admin/articlecat/');
            $this->view->ur_here = '分类添加';
        }

        /* 提交信息处理 */
        if ($this->_request->isPost()) {
            $filter = new Zend_Filter_StripTags();
            $data = array(
                'cat_name' => $filter->filter($this->_request->getPost('cat_name')),
                'parentid' => $filter->filter($this->_request->getPost('parentid')),
                'keywords' => $filter->filter($this->_request->getPost('keywords')),
                'cat_desc' => $filter->filter($this->_request->getPost('desc'))
            );
            $act = $this->_request->getPost('act');
            $uri = $this->_request->getPost('uri');

            if (!$act) { /* 添加 */
                /* 检查是否存在 */
                if (!$this->_articlecat->catExists($data)) {
                    $this->_articlecat->catInsert($data) ? $this->common->msg($uri, '分类添加成功！') : $this->common->msg($uri, '信息添加异常！', 'Error');
                } else {
                    $this->common->msg(urldecode($uri), '此分类以及存在！', 'Error');
                }
            } else { /* 修改保存 */
                /* 检查此分类下,除了此类是否与其他类别名称相同 */
                $catData = $data;
                $thisUrl = $filter->filter($this->_request->getPost('this_url'));
                $catData['cat_id'] = $filter->filter($this->_request->getPost('cat_id'));
                $catData['act'] = 'update';
                if (!$this->_articlecat->catExists($catData)) {
                    //$this->_articlecat->catUpdate($data, $catData['cat_id']) ? $this->common->msg($uri, '信息更新成功！') : $this->common->msg(urldecode($thisUrl), '信息更新异常，请稍候再试！', 'Error');
                    $this->_articlecat->catUpdate($data, $catData['cat_id']);
                    $this->common->msg($uri, '信息更新成功！');
                } else {
                    $this->common->msg(urldecode($thisUrl), '此分类名称在此父类下已经存在！', 'Error');
                }
            }
        }
    }

    /* 分类删除 */

    public function delAction() {
        /* 禁用视图 */
        $this->_helper->viewRenderer->setNoRender();
        $filter = new Zend_Filter_StripTags();
        $cat_id = $filter->filter($this->_request->get('cat_id'));
        $fromUrl = $filter->filter($this->_request->get('fromurl'));
        if (!empty($cat_id) && !empty($fromUrl)) {
            $this->_articlecat->catDelete($cat_id) ? $this->common->msg($fromUrl, '信息删除成功！') : $this->common->msg($fromUrl, '信息删除时出现异常,删除失败！', 'Error');
        }
    }

}

